package com.imut.hcdp.service.impl;

import java.util.Date;
import java.util.List;

import com.imut.hcdp.config.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imut.hcdp.mapper.ConsumerMapper;
import com.imut.hcdp.domain.Consumer;
import com.imut.hcdp.service.IConsumerService;
import org.springframework.util.CollectionUtils;


@Service
public class ConsumerServiceImpl implements IConsumerService
{
    @Autowired
    private ConsumerMapper consumerMapper;


    @Override
    public Consumer selectConsumerById(Long id)
    {
        return consumerMapper.selectConsumerById(id);
    }


    @Override
    public List<Consumer> selectConsumerList(Consumer consumer)
    {
        return consumerMapper.selectConsumerList(consumer);
    }


    @Override
    public int insertConsumer(Consumer consumer)
    {
        List<Consumer> consumers = selectConsumerList(new Consumer().setCard(consumer.getCard()));
        if(!CollectionUtils.isEmpty(consumers)){
            throw new ServiceException("身份证号已注册!");
        }
        List<Consumer> consumers1 = selectConsumerList(new Consumer().setPhone(consumer.getPhone()));
        if(!CollectionUtils.isEmpty(consumers1)){
            throw new ServiceException("手机号已注册!");
        }
        consumer.setCrttime(new Date());
        return consumerMapper.insertConsumer(consumer);
    }


    @Override
    public int updateConsumer(Consumer consumer)
    {
        List<Consumer> consumers = selectConsumerList(new Consumer().setCard(consumer.getCard()));
        if(!CollectionUtils.isEmpty(consumers) && !consumers.get(0).getId().equals(consumer.getId())){
            throw new ServiceException("身份证号已注册!");
        }

        List<Consumer> consumers1 = selectConsumerList(new Consumer().setCard(consumer.getPhone()));
        if(!CollectionUtils.isEmpty(consumers1) && !consumers.get(0).getId().equals(consumer.getId())){
            throw new ServiceException("手机号已注册!");
        }
        return consumerMapper.updateConsumer(consumer);
    }


    @Override
    public int deleteConsumerByIds(Long[] ids)
    {
        return consumerMapper.deleteConsumerByIds(ids);
    }


    @Override
    public int deleteConsumerById(Long id)
    {
        return consumerMapper.deleteConsumerById(id);
    }
}
